package com.ruoyi.biz.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.biz.domain.BizTrainIcon;
import com.ruoyi.biz.domain.BizTrainInOutRecord;
import com.ruoyi.biz.service.IBizTrainIconService;
import com.ruoyi.biz.service.IBizTrainInOutRecordService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.biz.mapper.BizStockyardTrainPositionMapper;
import com.ruoyi.biz.domain.BizStockyardTrainPosition;
import com.ruoyi.biz.service.IBizStockyardTrainPositionService;

/**
 * 整备场火车位置信息Service业务层处理
 * 
 * @author cc
 * @date 2025-11-11
 */
@Service
public class BizStockyardTrainPositionServiceImpl implements IBizStockyardTrainPositionService 
{
    @Autowired
    private BizStockyardTrainPositionMapper bizStockyardTrainPositionMapper;
    @Autowired
    private IBizTrainInOutRecordService bizTrainInOutRecordService;
    @Autowired
    private IBizTrainIconService bizTrainIconService;

    /**
     * 查询整备场火车位置信息
     * 
     * @param id 整备场火车位置信息主键
     * @return 整备场火车位置信息
     */
    @Override
    public BizStockyardTrainPosition selectBizStockyardTrainPositionById(Long id)
    {
        return bizStockyardTrainPositionMapper.selectBizStockyardTrainPositionById(id);
    }

    /**
     * 查询整备场火车位置信息列表
     * 
     * @param bizStockyardTrainPosition 整备场火车位置信息
     * @return 整备场火车位置信息
     */
    @Override
    public List<BizStockyardTrainPosition> selectBizStockyardTrainPositionList(BizStockyardTrainPosition bizStockyardTrainPosition)
    {
        return bizStockyardTrainPositionMapper.selectBizStockyardTrainPositionList(bizStockyardTrainPosition);
    }

    /**
     * 新增整备场火车位置信息
     * 
     * @param bizStockyardTrainPosition 整备场火车位置信息
     * @return 结果
     */
    @Override
    public int insertBizStockyardTrainPosition(BizStockyardTrainPosition bizStockyardTrainPosition)
    {
        bizStockyardTrainPosition.setCreateTime(DateUtils.getNowDate());
        return bizStockyardTrainPositionMapper.insertBizStockyardTrainPosition(bizStockyardTrainPosition);
    }

    /**
     * 修改整备场火车位置信息
     * 
     * @param bizStockyardTrainPosition 整备场火车位置信息
     * @return 结果
     */
    @Override
    public int updateBizStockyardTrainPosition(BizStockyardTrainPosition bizStockyardTrainPosition)
    {
        bizStockyardTrainPosition.setUpdateTime(DateUtils.getNowDate());
        return bizStockyardTrainPositionMapper.updateBizStockyardTrainPosition(bizStockyardTrainPosition);
    }

    /**
     * 批量删除整备场火车位置信息
     * 
     * @param ids 需要删除的整备场火车位置信息主键
     * @return 结果
     */
    @Override
    public int deleteBizStockyardTrainPositionByIds(Long[] ids)
    {
        return bizStockyardTrainPositionMapper.deleteBizStockyardTrainPositionByIds(ids);
    }

    /**
     * 删除整备场火车位置信息信息
     * 
     * @param id 整备场火车位置信息主键
     * @return 结果
     */
    @Override
    public int deleteBizStockyardTrainPositionById(Long id)
    {
        return bizStockyardTrainPositionMapper.deleteBizStockyardTrainPositionById(id);
    }

    /**
     * 获取图纸上的所有小火车图标
     * @param drawingId
     * @return
     */
    @Override
    public List<BizStockyardTrainPosition> getAllByDrawing(Long drawingId) {
        BizStockyardTrainPosition queryParam = new BizStockyardTrainPosition();
        queryParam.setDrawingId(drawingId);
        return this.selectBizStockyardTrainPositionList(queryParam);
    }

    /**
     * 放置火车
     * @param bizStockyardTrainPosition
     * @return
     */
    @Override
    public boolean enterTrain(BizStockyardTrainPosition bizStockyardTrainPosition, LoginUser user) {
        bizStockyardTrainPosition.setCrewId(user.getUserId());
        bizStockyardTrainPosition.setCrewWorkNumber(user.getUsername());
        bizStockyardTrainPosition.setCrewName(user.getUser().getNickName());
        bizStockyardTrainPosition.setEntryTime(new Date());
        this.insertBizStockyardTrainPosition(bizStockyardTrainPosition);


        BizTrainIcon icon = bizTrainIconService.selectBizTrainIconById(bizStockyardTrainPosition.getIconId());

        BizTrainInOutRecord record = new BizTrainInOutRecord();
        record.setDeptId(bizStockyardTrainPosition.getDeptId());
        record.setDrawingId(bizStockyardTrainPosition.getDrawingId());
        record.setLineId(bizStockyardTrainPosition.getLineId());
        record.setLineName(bizStockyardTrainPosition.getLineCode()); // todo 后续优化
        record.setLineCode(bizStockyardTrainPosition.getLineCode());
        record.setTrainModelId(bizStockyardTrainPosition.getTrainModelId());
        record.setIconId(bizStockyardTrainPosition.getIconId());
        record.setIconName(icon.getIconName());
        record.setTrainStatus(bizStockyardTrainPosition.getTrainStatus());
        record.setPlacement(bizStockyardTrainPosition.getPlacement());
        record.setTrainNumber(bizStockyardTrainPosition.getTrainNumber());
        record.setCrewId(bizStockyardTrainPosition.getCrewId());
        record.setCrewName(bizStockyardTrainPosition.getCrewName());
        record.setCrewWorkNumber(bizStockyardTrainPosition.getCrewWorkNumber());
        record.setConfirmerId(bizStockyardTrainPosition.getConfirmerId());
        record.setConfirmerWorkNumber(bizStockyardTrainPosition.getConfirmerWorkNumber());
        record.setConfirmerName(bizStockyardTrainPosition.getConfirmerName());
        record.setEntryTime(bizStockyardTrainPosition.getEntryTime());
        record.setDescription(bizStockyardTrainPosition.getDescription());
        bizTrainInOutRecordService.insertBizTrainInOutRecord(record);
        return false;
    }

    /**
     * 火车离开
     * @param bizStockyardTrainPosition
     * @return
     */

    @Override
    public boolean exitTrain(BizStockyardTrainPosition bizStockyardTrainPosition, LoginUser user) {


        bizStockyardTrainPosition = this.selectBizStockyardTrainPositionById(bizStockyardTrainPosition.getId());
        BizTrainIcon icon = bizTrainIconService.selectBizTrainIconById(bizStockyardTrainPosition.getIconId());
        bizStockyardTrainPosition.setTrainStatus("LEAVE");

        BizTrainInOutRecord record = new BizTrainInOutRecord();
        record.setDeptId(bizStockyardTrainPosition.getDeptId());
        record.setDrawingId(bizStockyardTrainPosition.getDrawingId());
        record.setLineId(bizStockyardTrainPosition.getLineId());
        record.setLineName(bizStockyardTrainPosition.getLineCode()); // todo 后续优化
        record.setLineCode(bizStockyardTrainPosition.getLineCode());
        record.setTrainModelId(bizStockyardTrainPosition.getTrainModelId());
        record.setIconId(bizStockyardTrainPosition.getIconId());
        record.setIconName(icon.getIconName());
        record.setTrainStatus(bizStockyardTrainPosition.getTrainStatus());
        record.setPlacement(bizStockyardTrainPosition.getPlacement());
        record.setTrainNumber(bizStockyardTrainPosition.getTrainNumber());
        record.setCrewId(bizStockyardTrainPosition.getCrewId());
        record.setCrewName(bizStockyardTrainPosition.getCrewName());
        record.setCrewWorkNumber(bizStockyardTrainPosition.getCrewWorkNumber());
        record.setConfirmerId(bizStockyardTrainPosition.getConfirmerId());
        record.setConfirmerWorkNumber(bizStockyardTrainPosition.getConfirmerWorkNumber());
        record.setConfirmerName(bizStockyardTrainPosition.getConfirmerName());
        record.setEntryTime(bizStockyardTrainPosition.getEntryTime());
        record.setLeaveTime(new Date());
        record.setDescription(bizStockyardTrainPosition.getDescription());
        bizTrainInOutRecordService.insertBizTrainInOutRecord(record);
        this.deleteBizStockyardTrainPositionById(bizStockyardTrainPosition.getId());
        return false;
    }
}
